package com.xunfang.cloud.controller;

import cn.hutool.core.util.IdUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class PayCircuitController {

    //=========Resilience4j CircuitBreaker 的例子
    @GetMapping(value = "/pay/circuit/{id}")
    public String myCircuit(@PathVariable("id") Integer id) {

        //抛异常
        if(id == -4) {
            throw new RuntimeException("----circuit id 不能负数");
        }
        //没有返回结果 需要等5s
        if(id == 9999){
            try { 
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            }
        }
        //正常返回结果
        return "Hello, circuit! inputId:  "+id+" \t " + IdUtil.simpleUUID();
    }
}